
<template>
  <div>
    <Card ref="card">
      <p slot="title">评估报告-{{ groupname }}</p>
      <Row :gutter="16">
        <Col span="3">
        <Select v-model="town" size="large" placeholder="选择乡镇">
          <Option v-for="item in townList" :value="item.TownName" :key="item.TownName">{{ item.TownName }}</Option>
        </Select>
        </Col>

        <Col span="3">
        <Select @on-change="Search()" v-if="false" v-model="AssessType" size="large" placeholder="评估类型">
          <Option v-for="item in AssessTypeList" :value="item.value" :key="item.value">{{ item.label }}</Option>
        </Select>
        </Col>
        <Col span="4">
        <Input v-model="pName" size="large" placeholder="姓名/身份证号/住址" />
        </Col>

        <Col span="4">
        <Button @click="Search" size="large" type="primary" icon="ios-search">查询</Button>
        </Col>

        <Col span="2">
        <Button @click="InputPrintTitle" size="large" type="primary" icon="md-print">打印</Button>
        </Col>
        <Col span="4">
        <Button @click="Export" size="large" type="primary" :loading="exportLoading" icon="md-download">导出</Button>
        </Col>

        <!-- <Col span="2">
          <Button @click="PrintReport" size="large" type="primary" icon="md-print">打印评估报告</Button>
        </Col> -->
      </Row>
    </Card>

    <iframe id="iframe-print" frameborder="0" style="display:none" scrolling="auto" :src="printSrc1"></iframe>
    <iframe id="iframe-print-report" frameborder="0" style="display:none" scrolling="auto" :src="printSrc2"></iframe>
    <iframe id="iframe-print-report2" frameborder="0" style="display:none" scrolling="auto" :src="printSrc3"></iframe>
    <iframe id="iframe-print-report3" frameborder="0" style="display:none" scrolling="auto" :src="printSrc4"></iframe>
    <iframe id="iframe-print-report4" frameborder="0" style="display:none" scrolling="auto" :src="printSrc5"></iframe>
    <iframe id="iframe-print-reportNew" frameborder="0" style="display:none" scrolling="auto" :src="printSrcNew"></iframe>
    <Row class="margin-top-10">
      <Table border :columns="tableTitle" :data="tableData" ref="table" :height="tableHeight"></Table>
    </Row>
    <Row class="margin-top-10">
      <Page :total="total" :current="pPage" :page-size="pSize" :page-size-opts="[50, 100, 300, 500, 1000, 2000, 5000]"
        @on-change="s => { pPage = s; Search(); }" @on-page-size-change="p => { pSize = p; pPage = 1; Search() }" show-total show-sizer />
    </Row>
    <Modal :mask-closable="false" v-model="modal2" width="500">
      <p slot="header" class="color-primary text-align-center">
        <Icon type="ios-information-circle"></Icon>
        <span>请输入打印标题</span>
      </p>
      <Row>
        <Input v-model="pringtTitle" size="large" placeholder="打印标题" />
      </Row>
      <br />

      <div slot="footer">
        <Button type="primary" size="large" v-on:click="Print">确定</Button>
      </div>
    </Modal>
  </div>
</template>
<script>
import excel from "@/libs/excel";
import { printUrl } from "@/libs/util";
import { Message, Modal } from "view-design";
import {
  GetGroupTown,
  SearchSavedPersonByApp,
  SaveActionLog
} from "@/api/data";
export default {
  name: "search_area_savedperson",
  data() {
    return { 

      printSrc1: printUrl + "printlist.html",
      printSrc2: printUrl + "printlistreport.html",
      printSrc3: printUrl + "printlistreport2.html",
      printSrc4: printUrl + "printlistreport3.html",
      printSrc5: printUrl + "printlistreport4.html",
      printSrcNew: printUrl + "printlistreportnew.html",
      AssessType: -1,
      AssessTypeList: [
        { label: "评估类型", value: -1 },
        // { label: "简易评估", value: 0 },
        { label: "标准评估", value: 1 },
        { label: "标准评估(新)", value: 2 },
        { label: "特困评估", value: 3 },
        { label: "特困评估(新)", value: 4 }
      ],
      total: 0,
      pPage: 1,
      pSize: 50,
      tableHeight: 450,
      modal2: false,
      personTemp: {},
      exportLoading: false,
      tableTitle: [
        {
          title: "序号",
          key: "R_INDEX",
          align: "center",
          width: 80
        },
        {
          title: "姓名",
          key: "Name",
          align: "center",
          width: 120
        },
        {
          title: "性别",
          key: "Sex",
          align: "center",
          width: 100
        },
        {
          title: "住址",
          key: "Address"
        },
        {
          title: "身份证号",
          key: "IdCard"
        },
        {
          title: "状态",
          align: "center",
          key: "FinalState",
          width: 120,
          render: (h, rparams) => {
            if (rparams.row.FinalState == null) {
              return h(
                "Tag",
                {
                  props: {
                    color: "default"
                  }
                },
                "待评估"
              );
            } else {
              var ctype = "default";
              if (rparams.row.FinalState == "重度失能" || rparams.row.FinalState == "失能") {
                ctype = "error";
              } else if (rparams.row.FinalState == "中度失能" || rparams.row.FinalState == "半自理") {
                ctype = "warning";
              } else if (rparams.row.FinalState == "轻度失能") {
                ctype = "primary";
              } else if (rparams.row.FinalState == "能力完好" || rparams.row.FinalState == "自理") {
                ctype = "success";
              }
              return h(
                "Button",
                {
                  props: {
                    type: ctype,
                    size: "small"
                  },
                  on: {
                    click: () => {
                      if (
                        rparams.row.AssessInfo &&
                        rparams.row.AssessInfo.Gid
                      ) {
                        //简易评估
                        if (rparams.row.AssessInfo.AssessType == 0) {
                          this.$router.push({
                            name: "assess_info",
                            params: { id: rparams.row.Gid }
                          });
                        }
                        //标准评估
                        else if (rparams.row.AssessInfo.AssessType == 1) {
                          this.$router.push({
                            name: "standard_assess_info",
                            params: { id: rparams.row.Gid }
                          });
                        } //新标准评估
                        else if (rparams.row.AssessInfo.AssessType == 2) {
                          this.$router.push({
                            name: "assess_new_info",
                            params: { id: rparams.row.Gid }
                          });
                        }  //特困评估
                        else if (rparams.row.AssessInfo.AssessType == 3) {
                          this.$router.push({
                            name: "tekun_assess_info",
                            params: { id: rparams.row.Gid }
                          });
                        }
                         //新特困评估
                      else if (rparams.row.AssessInfo.AssessType == 4) {
                        this.$router.push({
                          name: "tekun_new_assess_info",
                          params: { id: rparams.row.Gid }
                        });
                      }
                      }
                    }
                  }
                },
                rparams.row.FinalState
              );
            }
          }
        },
        {
          title: "备注",
          key: "Remark"
        }
      ],
      groupname: "",
      pringtTitle: "",
      tableData: [],
      pName: "",
      town: "",
      townList: []
    };
  },
  props: {
    id: {
      type: String,
      required: true
    }
  },
  watch: {
    town: function (newQuestion, oldQuestion) {
      this.Search();
    }
  },
  methods: {
    GetGroupTown() {
      let vm = this;
      GetGroupTown(vm.id).then(res => {
        const data = res.data;
        if (data.Type == 1) {
          vm.townList = data.Data;
        } else {
          this.$Modal.error({ title: "提示", content: data.Message });
        }
      });
    },
    Search() {
      let vm = this;
      var reqData = {
        page: vm.pPage,
        size: vm.pSize,
        groupid: vm.id,
        grouptown: vm.town,
        state: 1,
        str: vm.pName,
        assesstype: vm.AssessType
      };
      SearchSavedPersonByApp(reqData).then(res => {
        const data = res.data;
        if (data.Type == 1) {
          vm.total = data.Data.Total;
          vm.tableData = data.Data.Rows;
          if (vm.AssessType == 0) {
            document
              .getElementById("iframe-print-report")
              .contentWindow.LoadData(vm.tableData);
          } else if (vm.AssessType == 1) {
            document
              .getElementById("iframe-print-report2")
              .contentWindow.LoadData(vm.tableData);
          } else if (vm.AssessType == 2) {
            document
              .getElementById("iframe-print-reportNew")
              .contentWindow.LoadData(vm.tableData);
          } else if (vm.AssessType == 3) {
            document
              .getElementById("iframe-print-report3")
              .contentWindow.LoadData(vm.tableData);
          } else if (vm.AssessType == 4) {
            document
              .getElementById("iframe-print-report4")
              .contentWindow.LoadData(vm.tableData);
          }
        } else {
          Message.error(data.Message);
        }
      });
    },
    Export() {
      if (this.tableData.length) {
        this.exportLoading = true;
        const params = {
          title: [
            "序号",
            "姓名",
            "性别",
            "住址",
            "身份证号",
            "状态",
            "是否新增",
            "备注"
          ],
          key: [
            "R_INDEX",
            "Name",
            "Sex",
            "Address",
            "IdCard",
            "FinalState",
            "IsNew",
            "Remark"
          ],
          data: this.tableData,
          autoWidth: true,
          filename: this.groupname + this.town
        };
        excel.export_array_to_excel(params);
        this.exportLoading = false;
        this.SaveActionLog("导出", this.groupname);
      } else {
        this.$Message.info("表格数据不能为空！");
      }
    },
    InputPrintTitle() {
      if (!this.pringtTitle) {
        this.pringtTitle = this.groupname;
      }
      this.modal2 = true;
    },
    Print() {
      this.modal2 = false;
      var tableobj = {
        Table: this.tableData
      };
      document
        .getElementById("iframe-print")
        .contentWindow.printData(this.pringtTitle, JSON.stringify(tableobj));
      this.SaveActionLog("打印", this.groupname);
    },
    PrintReport() {
      if (this.AssessType == -1) {
        this.$Message.info("请先选择评估类型");
        return;
      }
      if (this.tableData.length > 100) {
        this.$Message.info("一次最多只能打印100条数据");
        return;
      }
      if (this.AssessType == 0) {
        document
          .getElementById("iframe-print-report")
          .contentWindow.printData();
        this.SaveActionLog("打印", this.groupname + "评估报告");
      } else if (this.AssessType == 1) {
        document
          .getElementById("iframe-print-report2")
          .contentWindow.printData();
        this.SaveActionLog("打印", this.groupname + "评估报告");
      }  else if (this.AssessType == 2) {
        document
          .getElementById("iframe-print-reportNew")
          .contentWindow.printData();
        this.SaveActionLog("打印", this.groupname + "评估报告");
      } else if (this.AssessType == 3) {
        document
          .getElementById("iframe-print-report3")
          .contentWindow.printData();
        this.SaveActionLog("打印", this.groupname + "评估报告");
      } else if (this.AssessType == 4) {
        document
          .getElementById("iframe-print-report4")
          .contentWindow.printData();
        this.SaveActionLog("打印", this.groupname + "评估报告");
      }
    },
    SaveActionLog(vaction, vfield) {
      var reqData = {
        Action: vaction,
        Filed: vfield
      };
      SaveActionLog(reqData).then(res => { });
    }
  },
  created() { },
  mounted() {
    var cardHeight = this.$refs.card.$el.offsetHeight;
    this.tableHeight = window.innerHeight - cardHeight - 200;
    this.groupname = this.$route.query.groupname;
    this.GetGroupTown();
    this.Search();
  }
};
</script>
